package com.wumeng;

import com.wumeng.entity.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        System.out.println("-----------------获取 Person 类的 Class 对象----------------");
        //获取 Person 类的 Class 对象
        Class aClass = Class.forName("com.wumeng.entity.Person");


        System.out.println(aClass);
        System.out.println("-----------------获取 Person 类的所有方法信息----------------");

        //获取 Person 类的所有方法信息
        Method[] methods = aClass.getDeclaredMethods();

        for (Method method : methods) {
            System.out.println(method);

        }

        System.out.println("-----------------获取 Person 类的所有成员属性信息----------------");

        //获取 Person 类的所有成员属性信息
        Field[] field = aClass.getDeclaredFields();
        for (Field f : field) {
            System.out.println(f.toString());
        }

        System.out.println("-----------------获取 Person 类的所有构造方法信息----------------");


        //获取 Person 类的所有构造方法信息
        Constructor[] constructor = aClass.getDeclaredConstructors();
        for (Constructor c : constructor) {
            System.out.println(c.toString());
        }


        // 使用.newInstance();创建对象
//        Person person = (Person) aClass.newInstance();
//
//
//        System.out.println(person.getAge());

        //获取构造方法并创建对象
        Constructor c = aClass.getDeclaredConstructor(String.class, Integer.class);
        //创建对象并设置属性
        Person p1 = (Person) c.newInstance("李四", 20);

        System.out.println(p1.getAge());
    }
}
